
\\ sieve of Eratosthenes in 3 programming language

\ max: scalar=255;
{ {_DicLst`} 'max' _SCALAR 255 _DicNew` }}

\ n:vector[1..max] of byte; // all 0 to start
\\ ones left zero at end are prime
{ {_DicLst`} 'n' _DATA 
  { 0 {_VecLst`} 132 _VECBYT 2 _VecNew` } _DicNew` }}

\ i:int=2;
{ {_DicLst`} 'i' _SCALAR 2 _DicNew` }}

\ j:int;
{ {_DicLst`} 'j' _SCALAR 0 _DicNew` }}

\--------------------------------------------------------------------

\ i is prime, n[i] is zero; for all prime k<i; n[m*k] = 1 where m*k <= max

\ strike: fn():bool   \\ returns false iff j+=i <= max
{ {_DicLst`} 'strike' _SYMFN
' n@ j {[]`} 1 {_[_b`}} \\\
  { j@  j i + _DicSetScalVecP` }}      \\\
  max j { >>` } "     \\\
' _DicNew` }}

\ testnsubi():bool    \\ returns false if n[i] == 0
{ {_DicLst`} 'testnsubi' _SYMFN 
' n@ i {[]`} {_[b`} "  \\\
' _DicNew` }}

\ slide: fn():bool \\ ret false iff i > max orelse n[i] == 0
{ {_DicLst`} 'slide' _SYMFN
' { i@ i 1 +  _DicSetScalVecP` }}  \\\
  { testnsubi@       \\\
  max i  {<=`}        \\\
  :]  } "             \\\
' _DicNew` }}

\ mark: fn():bool   \\ returns false iff i < max 
\                   \\ and n[i] == 0
{ {_DicLst`} 'mark' _SYMFN
' { j@ i i + _DicSetScalVecP` }}    \\ j = i + i \\\
  strike@ {} {[_`}       \\   while not strike() do \\\
  slide@ {~} {[_`}       \\ while slide() do \\\
  max i {>>`}  "          \\\
' _DicNew` }}

\ prtn: fn(i):null  \\ prints ith elt of n
{ {_DicLst`} 'prtn' _SYMFN
' { 3 :: {_[`} \\\
  { 1 :: {_[`}  #20 {][`} 3 _PrtDispChLong` }} \\\
  n@ {][`} {[]`} {_[b`}   \\\
  #20 {][`} 2 _PrtDispChLong` }} {_PrtGetRawKey`}}  \\\
  { \'\\0a\\0d\' _Prt`} " \\\
' _DicNew` }}

{ mark@ {} {[_`} prtn@ max 1 + {%} {[_`} {'Eratosthenes salutes you.' _Prt`}} }

    
     

